package com.melody.controller;

import com.melody.entry.Result;
import com.melody.entry.UserInfo;
import com.melody.model.dto.UserSaveDto;
import com.melody.model.dto.UserRegisterDto;
import com.melody.model.vo.UserInfoVo;
import com.melody.model.vo.UserLogin;
import com.melody.service.UserInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zengxz
 * @Description: 用户信息
 * @DateTime: 2025-05-08 21:58
 **/
@RestController
@RequestMapping("/user")
public class UserInfoController {


    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户注册
     * @param userRegisterDto
     * @return
     */
    @PostMapping("/register")
    public Result<String> userRegister(@RequestBody UserRegisterDto userRegisterDto){
        return userInfoService.userRegister(userRegisterDto);
    }


    /**
     * 账号登录
     * @param userSaveDto
     * @return
     */
    @PostMapping("/info")
    public Result<String> userLogin(@RequestBody UserSaveDto userSaveDto) {
        return  userInfoService.userLogin(userSaveDto);
    }

    /**
     * 验证码登录
     */
    @PostMapping("/codeLogin")
    public Result<String> codeLogin(@RequestBody UserSaveDto userSaveDto) {
        return  userInfoService.codeLogin(userSaveDto);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody UserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return Result.success("更新成功");
    }

    /**
     * 删除用户信息（根据ID）
     */
    @DeleteMapping("/delete")
    public Result<String> deleteUser() {
        return  userInfoService.deleteUser();
    }
/**
 * 根据id删除用户
 */
    @DeleteMapping("/deleteById")
    public Result<String> deleteUserById(@RequestParam Integer userId) {
        return userInfoService.removeById(userId) ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 查询自身用户信息
     */
    @GetMapping("/userInfo")
    public Result<UserInfoVo> userInfo() {
        return userInfoService.userInfoById();
    }

    /**
     * 根据id查询用户信息
     * @return
     */
    @GetMapping("/userInfoById")
    public Result<UserInfoVo> userInfoById(@RequestParam Integer userId) {
        return userInfoService.userInfoByIdid(userId);
    }

    /**
     * 查询所有用户信息
     */
    @GetMapping
    public Result<List<UserInfo>> getAll() {
        return Result.success(userInfoService.list());
    }


    /**
     * 天气项目临时借用
     * 登录/注册
     * 用完删
     */
    @PostMapping("/loginOrRegister")
    public Result<String> loginOrRegister(@RequestBody UserLogin userLogin) {
        return userInfoService.loginOrRegister(userLogin) ;
    }
}

